package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.TkfDaoI;
import jbl.model.Tkf;
import jbl.pageModel.DataGrid;
import jbl.pageModel.Kf;
import jbl.service.KfServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("kfService")
public class KfServiceImpl implements KfServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(KfServiceImpl.class);
	
	private TkfDaoI kfDao;
	
	@Autowired
	public void setKfDao(TkfDaoI kfDao) {
		this.kfDao = kfDao;
	}

	@Override
	public DataGrid dataGrid(Kf kf) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		try {
			List<Kf> kfList = new ArrayList<Kf>();
			//Map<String, Object> params = new HashMap<String, Object>();
			String hql = " From Tkf t where 1=1 ";
			String totalHql = "select count(*) " + hql;
			hql = addWhere(hql, kf);
			hql = addOrder(hql , kf);
			List<Tkf> list = kfDao.find(hql,kf.getPage(), kf.getRows()); 
			if(null != list && list.size() > 0){
				for(Tkf t : list){
					Kf kf1 = new Kf();
					BeanUtils.copyProperties(t, kf1);
					kfList.add(kf1);
				}
			}
			dg.setRows(kfList);
			dg.setTotal(kfDao.count(totalHql));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return dg;
	}
	
	private String addOrder(String hql , Kf kf){
		if(null != kf.getSort()){
			hql += " order by t."+kf.getSort() +" " + kf.getOrder();
		}
		return hql;
	}
	
	private String addWhere(String hql , Kf kf){
		if(null != kf.getNumberID() && !"".equals(kf.getNumberID())){
			hql += " and t.numberID like '%"+kf.getNumberID()+"%'";
		}
		return hql;	
	}

	@Override
	public Json update(Kf kf) {
		Json j = new Json();
		Tkf tkf = new Tkf();
		try {
			BeanUtils.copyProperties(kf, tkf);
			
			kfDao.update(tkf);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("修改库房error:"+e.getMessage());
			j.setMsg("修改失败");
		}
		return j;
	}

	@Override
	public Json add(Kf kf) {
		Json j = new Json();
		Tkf tkf = new Tkf();
		try {
			BeanUtils.copyProperties(kf, tkf);
			tkf.setId(Encrypt.createUUID());
			
			kfDao.save(tkf);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("添加库房error:"+e.getMessage());
			j.setMsg("添加失败");
		}
		return j;
	}

	@Override
	public List<Kf> comboboxInfo() {
		// TODO Auto-generated method stub
		List<Kf> list = new ArrayList<Kf>();
		try {
			String hql = " from Tkf t where t.flag = 'T' ";
			List<Tkf> tkfList = kfDao.find(hql);
			if(null != tkfList && tkfList.size() > 0){
				for(Tkf f : tkfList){
					Kf kf = new Kf();
					BeanUtils.copyProperties(f, kf);
					list.add(kf);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("查询库房error:"+e.getMessage());
		}
		return list;
	}
}
